package com.chenyaowei.web.controller.member;

import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.annotation.RepeatSubmit;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.member.domain.CywMemberCollectSpu;
import com.chenyaowei.member.service.ICywMemberCollectSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员收藏的商品Controller
 *
 * @author 陈耀威
 * @date 2023-12-03
 */
@RestController
@RequestMapping("/member/collectSpu")
public class CywMemberCollectSpuController extends BaseController
{
    @Autowired
    private ICywMemberCollectSpuService cywMemberCollectSpuService;

    /**
     * 查询会员收藏的商品列表
     */
    @GetMapping("/list/currentUser")
    public TableDataInfo listByMemberId(CywMemberCollectSpu cywMemberCollectSpu)
    {
        startPage();
        cywMemberCollectSpu.setMemberId(getUserId());
        List<CywMemberCollectSpu> list = cywMemberCollectSpuService.selectCywMemberCollectSpuList(cywMemberCollectSpu);
        return getDataTable(list);
    }

    /**
     * 查询会员收藏的商品列表
     */
//    @PreAuthorize("@ss.hasPermi('member:collectSpu:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywMemberCollectSpu cywMemberCollectSpu)
    {
        startPage();
        List<CywMemberCollectSpu> list = cywMemberCollectSpuService.selectCywMemberCollectSpuList(cywMemberCollectSpu);
        return getDataTable(list);
    }

    /**
     * 导出会员收藏的商品列表
     */
    @PreAuthorize("@ss.hasPermi('member:collectSpu:export')")
    @Log(title = "会员收藏的商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywMemberCollectSpu cywMemberCollectSpu)
    {
        List<CywMemberCollectSpu> list = cywMemberCollectSpuService.selectCywMemberCollectSpuList(cywMemberCollectSpu);
        ExcelUtil<CywMemberCollectSpu> util = new ExcelUtil<CywMemberCollectSpu>(CywMemberCollectSpu.class);
        util.exportExcel(response, list, "会员收藏的商品数据");
    }

    /**
     * 获取会员收藏的商品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('member:collectSpu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cywMemberCollectSpuService.selectCywMemberCollectSpuById(id));
    }

    /**
     * 新增会员收藏的商品
     */
//    @PreAuthorize("@ss.hasPermi('member:collectSpu:add')")
    @RepeatSubmit(interval = 3000)
    @Log(title = "会员收藏的商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CywMemberCollectSpu cywMemberCollectSpu)
    {
        return toAjax(cywMemberCollectSpuService.insertCywMemberCollectSpu(cywMemberCollectSpu));
    }

    /**
     * 修改会员收藏的商品
     */
//    @PreAuthorize("@ss.hasPermi('member:collectSpu:edit')")
    @Log(title = "会员收藏的商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywMemberCollectSpu cywMemberCollectSpu)
    {
        return toAjax(cywMemberCollectSpuService.updateCywMemberCollectSpu(cywMemberCollectSpu));
    }

    /**
     * 删除会员收藏的商品
     */
/*    @PreAuthorize("@ss.hasPermi('member:collectSpu:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cywMemberCollectSpuService.deleteCywMemberCollectSpuByIds(ids));
    }*/

    @RepeatSubmit(interval = 3000)
    @Log(title = "会员收藏的商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{spuId}")
    public AjaxResult remove(@PathVariable("spuId") Long spuId)
    {
        return toAjax(cywMemberCollectSpuService.deleteCurrentMemberCollectBySpuId(getUserId(), spuId));
    }
}
